package cn.stylefeng.guns.modular.agv.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.agv.model.params.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;


public class TustApiUtils {

    /**
     * 创建任务
     * @param baseUrl
     * @param param
     * @return
     */
    public static TuskRsVo createTask(String baseUrl, TuskAgvTaskParam param){
        // 接口地址
        String url = baseUrl + "/createTask";
        ArrayList<String> targets = new ArrayList<String>();
        targets.add(param.getSrcPositionCode());
        targets.add(param.getTarPositionCode());

        // 构建请求参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskId", param.getTaskId());
        jsonObject.put("targets", targets);
        jsonObject.put("priority", param.getPriority());
        jsonObject.put("configId", param.getConfigId());
        String rs = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(jsonObject))
                .execute().body();
        //System.out.println("createTask2:"+rs);
        return JSONUtil.toBean(rs, TuskRsVo.class);
    }


    /**
     * 创建拣选任务的接口
     * @param baseUrl
     * @param param
     * @return
     */
    public static TuskRsVo createSortTask(String baseUrl, CreateSortParam param){
        // 接口地址
        String url = baseUrl + "/createSortTask";
        // 构建请求参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskId", param.getTaskId());
        jsonObject.put("targetActions", param.getTargetActions());
        String rs = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(jsonObject))
                .execute().body();
        return JSONUtil.toBean(rs, TuskRsVo.class);
    }

    /**
     * 上层系统调用此接口变更AGV任务
     * @param baseUrl
     * @param param
     * @return
     */
    public static TuskRsVo changeTask(String baseUrl, ChangeTaskParam param){
        // 接口地址
        String url = baseUrl + "/changeTask";
        // 构建请求参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskId", param.getTaskId());
        jsonObject.put("target", param.getTarget());
        String rs = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(jsonObject))
                .execute().body();
        return JSONUtil.toBean(rs, TuskRsVo.class);
    }


    /**
     * 告知lothar亮灯和拣选数量
     * @param baseUrl
     * @param param
     * @return
     */
    public static TuskRsVo pickLighting(String baseUrl, PickLightParam param){
        // 接口地址
        String url = baseUrl + "/pickLighting";
        // 构建请求参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("storageName", param.getStorageName());
        jsonObject.put("aprNum", param.getAprNum());
        jsonObject.put("num", param.getNum());
        String rs = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(jsonObject))
                .execute().body();
        return JSONUtil.toBean(rs, TuskRsVo.class);
    }





    /**
     * 查询集货存放数据情况
     * @param baseUrl
     * @return
     */
    public static StorageRsVo getMapStorage(String baseUrl){
        // 接口地址
        String url = baseUrl + "/getMapStorage";
        // 构建请求参数
//        JSONObject jsonObject = new JSONObject();
//        if(StringUtils.isNotBlank(areaNames)){
//            jsonObject.put("areaNames", ListUtil.of(areaNames));
//        }
        String rs = HttpUtil.get(url);
        return JSONUtil.toBean(rs, StorageRsVo.class);
    }

    public static StorageRsVo getMapStorageByRoadway(String baseUrl,String roadway){
        // 接口地址
        String url = baseUrl + "/getMapStorage/roadway?roadway="+roadway;
        String rs = HttpUtil.get(url);
        return JSONUtil.toBean(rs, StorageRsVo.class);
    }

    /**
     * 清空通道
     * @param baseUrl
     * @param areaName
     * @return
     */
    public static FeedbackTaskRS clear(String baseUrl, String areaName){
        // 接口地址
        String url = baseUrl + "/area/clear";
        // 构建请求参数
        JSONObject jsonObject = new JSONObject();
        if(StringUtils.isNotBlank(areaName)){
            jsonObject.put("areaName", areaName);
        }
        String rs = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(jsonObject))
                .execute().body();
        return JSONUtil.toBean(rs, FeedbackTaskRS.class);
    }

    /**
     * 清空通道
     * @param baseUrl
     * @param areaName
     * @return
     */
    public static FeedbackTaskRS unlock(String baseUrl, String areaName){
        // 接口地址
        String url = baseUrl + "/unlock";
        // 构建请求参数
//        JSONObject jsonObject = new JSONObject();
        if(StringUtils.isNotBlank(areaName)){
//            jsonObject.put("areaName", areaName);
            url = url + "?areaName="+areaName;
                    //
        }
        String rs = HttpRequest.post(url)
//                .body(JSONUtil.toJsonStr(jsonObject))
                .execute().body();
        return JSONUtil.toBean(rs, FeedbackTaskRS.class);
    }

    /**
     * 优先级设置
     * @param baseUrl
     * @param priorityParam
     * @return
     */
    public static FeedbackTaskRS setPriority(String baseUrl, PriorityParamVo priorityParam){
        // 接口地址
        String url = baseUrl + "/setPriority";
        String rs = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(priorityParam))
                .execute().body();
        return JSONUtil.toBean(rs, FeedbackTaskRS.class);
    }

}
